package org.example.project.ui

import android.util.Log
import androidx.compose.animation.core.Animatable
import androidx.compose.animation.core.tween
import androidx.compose.foundation.background
import androidx.compose.foundation.gestures.detectVerticalDragGestures
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.unit.dp
import kotlinx.coroutines.launch

// 定义三个抽屉状态
enum class DrawerState {
    Collapsed,
    SemiExpanded,
    Expanded
}

/**
 * 通用版 TencentMapDrawer
 */
@Composable
internal fun TencentMapDrawer(
    modifier: Modifier = Modifier,
    backgroundContent: @Composable () -> Unit = {},  // 背景内容
    drawerContent: @Composable (DrawerState, Float) -> Unit, // 抽屉里的内容
    onCollapse: () -> Unit = {},
    onSemiExpand: () -> Unit = {},
    onExpand: () -> Unit = {}
) {
    val coroutineScope = rememberCoroutineScope()
    val progress = remember { Animatable(0f) }
    var drawerState by remember { mutableStateOf(DrawerState.Collapsed) }
    BoxWithConstraints(modifier = modifier.fillMaxSize()) {
        val screenHeight = maxHeight  // ✅ 不依赖 LocalConfiguration
    Box(modifier = modifier.fillMaxSize()) {
        // 显示背景内容
        backgroundContent()
        Surface(
            modifier = Modifier
                .fillMaxWidth()
//                .padding(horizontal = 8.dp, vertical = 6.dp) 去除左右的边距
//                .clip(RoundedCornerShape(topStart = 18.dp, topEnd = 18.dp))// ✅ 去除圆角
                .height(80.dp + screenHeight * 0.80f * progress.value)
                .align(Alignment.BottomCenter)
                .pointerInput(Unit) {
                    detectVerticalDragGestures(
                        onDragStart = {
                            Log.d("TencentMapDrawer", "开始拖动")
                        },
                        onDragEnd = {
                            Log.d("TencentMapDrawer", "拖动结束，当前 progress=${progress.value}")
                            coroutineScope.launch {
                                val target = when {
                                    progress.value < 0.3f -> DrawerState.Collapsed
                                    progress.value < 0.7f -> DrawerState.SemiExpanded
                                    else -> DrawerState.Expanded
                                }
                                Log.d("TencentMapDrawer", "目标状态：$target")
                                drawerState = target
                                val targetValue = when (target) {
                                    DrawerState.Collapsed -> 0f
                                    DrawerState.SemiExpanded -> 0.5f
                                    DrawerState.Expanded -> 1f
                                }
                                progress.animateTo(targetValue, tween(600))
                                Log.d("TencentMapDrawer", "动画结束，最终 progress=${progress.value}")
                                when (target) {
                                    DrawerState.Collapsed -> onCollapse()
                                    DrawerState.SemiExpanded -> onSemiExpand()
                                    DrawerState.Expanded -> onExpand()
                                }
                            }
                        },
                        onVerticalDrag = { change, dragAmount ->
                            change.consume()
                            val newProgress = (progress.value - dragAmount / (screenHeight.value * 0.85f)).coerceIn(0f, 1f)
                            coroutineScope.launch {
                                progress.snapTo(newProgress)
                                Log.d("TencentMapDrawer", "拖动中，dragAmount=$dragAmount, progress=${progress.value}")
                                Log.d("TencentMapDrawer", "拖动中，newProgress=$newProgress, 实际 progress=${progress.value}")
                            }
                        }
                    )
                },
            color = Color.White,
            elevation = 12.dp
        ) {
            drawerContent(drawerState, progress.value)
        }
    }
    }
}